package com.syqc.comlib.utils

import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
import android.util.Log
import com.syqc.comlib.R
import java.util.*

data class LanguageInfo(
    val type: String,
    val lang: String,
    val country: String,
    val desc: Int,
    val trans: String
)

object Language {
    private val languages = mutableListOf<LanguageInfo>()

    //选中的语言下标
    private var index = 0

    init {
        index = CacheMMKV.decodeInt("LanguageIndex", 0)
    }

    fun getIndex(): Int {
        return index
    }

    fun setIndex(index: Int) {
        Language.index = index
        CacheMMKV.encode("LanguageIndex", index)
    }

    //语言种类
    private var language = ""

    //当前语言是否是lang
    fun isCurrentLanguage(lang: String) = language == lang

    //返回只读语言列表
    fun getLanguages() = languages

    //语言类型描述
    fun getLangDesc() = languages[index].desc

    //是不是英文
    fun isEnglish() = language == "en_US"

    //获取当前语言类型
    fun getCurrentLanguage() = language


    fun init(context: Context) {
        val res = context.resources.getStringArray(R.array.languages)
        initLanguages(res)
        if (index == 0) getDefault()
    }

    private fun initLanguages(trans: Array<String>) {
        if (languages.isEmpty()) {
            languages.add(LanguageInfo("-1x", "-1x", "-1x", R.string.language_default, trans[0]))
            languages.add(LanguageInfo("zh_CN", "zh", "CN", R.string.language_zhcn, trans[1]))
            languages.add(LanguageInfo("zh_TW", "zh", "TW", R.string.language_zhtw, trans[2]))
            languages.add(LanguageInfo("en_US", "en", "US", R.string.language_enus, trans[3]))
            languages.add(LanguageInfo("fr_FR", "fr", "FR", R.string.language_frfr, trans[4]))
            languages.add(LanguageInfo("ar_AE", "ar", "AE", R.string.language_arae, trans[5]))
            languages.add(LanguageInfo("ru_RU", "ru", "RU", R.string.language_ruru, trans[6]))
            languages.add(LanguageInfo("es_ES", "es", "ES", R.string.language_eses, trans[7]))
            languages.add(LanguageInfo("pt_PT", "pt", "PT", R.string.language_ptpt, trans[8]))
            languages.add(LanguageInfo("vi_VN", "vi", "VN", R.string.language_vivn, trans[9]))
            languages.add(LanguageInfo("ja_JP", "ja", "JP", R.string.language_jajp, trans[10]))
            languages.add(LanguageInfo("th_TH", "th", "TH", R.string.language_thth, trans[11]))
            languages.add(LanguageInfo("nl_NL", "nl", "NL", R.string.language_nlnl, trans[12]))
        }
    }

    /** 获取系统默认语言 */
    private fun getDefaultLanguage(): Locale {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList.getDefault().get(0)
        } else {
            Locale.getDefault()
        }
    }

    /**
     * 判断是否是支持的语言
     *
     * @param locale
     * @return
     */
    private fun isSupport(locale: Locale): Boolean {
        val lang = locale.language
        for (language in languages) {

            if (lang == language.lang) return true
        }
        return false
    }

    //获取语言相关元素
    private fun getLangInfo(lang: String): LanguageInfo? {
        for (language in languages) {
            if (lang == language.lang) return language
        }
        return null
    }

    fun getDefault() {
        val default = getDefaultLanguage()
        if (isSupport(default)) {
            if (default.language == "zh") {
                val languageInfo = if (default.country == "CN") {
                    if (default.toString().contains("Hant")) languages[2] else languages[1]
                } else languages[2]
                language = languageInfo.type
            } else {
                val info = getLangInfo(default.language)
                info!!.run {
                    language = type
                }
            }
        } else {
            language = "en_US"
        }
    }


    private fun getLocale(): Locale {
        return if (index == 0) {
            val default = getDefaultLanguage()
            return if (isSupport(default)) {
                if (default.language == "zh") {
                    val language = if (default.country == "CN") {
                        if (default.toString().contains("Hant")) languages[2] else languages[1]
                    } else languages[2]
                    Locale(language.lang, language.country)
                } else {
                    val info = getLangInfo(default.language)
                    info!!.run {
                        language = type
                        Locale(lang, country)
                    }
                }
            } else {
                language = "en_US"
                Locale.ENGLISH
            }
        } else {
            languages[index].run {
                language = type
                Locale(lang, country)
            }
        }
    }

    private fun getLanguage(type: String): Locale {
        languages.forEach {
            if (it.type == type) return Locale(it.lang, it.country)
        }
        return Locale.ENGLISH
    }

    /** 设置语言Context */
    fun attachBaseContext(context: Context?, default: String = ""): Context? {
        if (context == null) return null
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            createResource(context, default)
        } else {
            updateResource(context, default)
        }
    }

    /** 7.0之后设置语言 */
    private fun createResource(context: Context, default: String): Context {
        val resources = context.resources
        val configuration: Configuration = resources.configuration
        val targetLocale: Locale = if (default.isEmpty()) getLocale() else getLanguage(default)
        Log.e("语言设置7.0之后", "====== $targetLocale")
        configuration.setLocale(targetLocale)
        resources.updateConfiguration(configuration, resources.displayMetrics)
        //创建配置
        return context.createConfigurationContext(configuration)
    }

    /** 7.0之前设置语言 */
    private fun updateResource(context: Context, default: String): Context {
        val targetLocale: Locale = if (default.isEmpty()) getLocale() else getLanguage(default)
        val configuration: Configuration = context.resources.configuration
        Log.e("语言设置", "====== $targetLocale")
        configuration.setLocale(targetLocale)
        val resources = context.resources
        //更新配置
        resources.updateConfiguration(configuration, resources.displayMetrics)
        return context
    }


}



